import 'package:flutter/material.dart';
import 'package:flutter_app_demo/core/widgets/counter_provider.dart';
import 'package:provider/provider.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';

import '../../core/constants/app_route_constants.dart';
import '../../core/manager/app_theme_manager.dart';
import 'package:go_router/go_router.dart';

import '../../dart/stream_test.dart';
import 'counter.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
  });

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _incrementCounter() {
    CountProvider.of(context).onIncremwnt();
  }

  Widget _buildSwitch() {
    bool isDark = Theme.of(context).brightness == Brightness.dark;
    AppThemeManager appThemeManager = AppThemeScope.read(context);
    return Switch(
      value: isDark,
      inactiveTrackColor: Colors.white,
      trackOutlineColor: MaterialStateProperty.all(Colors.transparent),
      thumbIcon: MaterialStateProperty.all(
          isDark ? const Icon(Icons.dark_mode) : const Icon(Icons.light_mode)),
      onChanged: appThemeManager.switchTheme,
    );
  }

  Widget _buildSettingButton() {
    return IconButton(
      onPressed: () => context.pushNamed(AppRouterConstants.themeSettingsPage),
      icon: const Icon(Icons.settings),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [_buildSwitch(), _buildSettingButton()],
        // backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'InheritedWidget点击按钮的次数:',
                ),
                Text(
                  'Count: ${CountProvider.of(context).count}',
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'Provider点击按钮的次数:',
                ),
                Consumer<CounterService>(
                  builder: (context, counter, child) {
                    return Text(
                      'Count: ${counter.count}',
                      style: Theme.of(context).textTheme.headlineMedium,
                    );
                  },
                ),
              ],
            ),
            StreamBuilder<String>(
              stream: generateMessages(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Text('result: ${snapshot.data}');
                } else {
                  return const CircularProgressIndicator();
                }
              },
            ),
          ],
        ),
      ),
      floatingActionButton: SpeedDial(
        animatedIcon: AnimatedIcons.menu_close,
        // 图标切换动画
        backgroundColor: Colors.pinkAccent,
        overlayColor: Colors.black,
        overlayOpacity: 0.5,
        // 背景透明度
        children: [
          SpeedDialChild(
            child: const Icon(Icons.add),
            label: 'Add by InheritedWidget',
            backgroundColor: Colors.green,
            onTap: () => _incrementCounter(),
          ),
          SpeedDialChild(
            child: const Icon(Icons.add),
            label: 'Add by Provider',
            backgroundColor: Colors.blue,
            onTap: () => context.read<CounterService>().increment(),
          ),
        ],
      ),
    );
  }
}
